package com.bodu.global.config.filter;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.bodu.core.cache.CacheService;
import com.bodu.core.entity.result.RStateEnum;
import com.bodu.core.entity.result.Result;
import com.bodu.global.security.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * token filter
 *
 * @author gaoya
 * @date 2022 - 10 - 10 23:01:19
 */
@Slf4j
public class TokenAuthenticationFilterConfig extends OncePerRequestFilter {

    /**
     * 系统登录地址
     */
    private final static String LOGIN_IN_PROCESS_URL = "/login/in";

    /**
     * 登录页面
     */
    private final static String LOGIN_PROCESS_URL = "/login";

    @Autowired
    private CacheService cacheService;

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        String token = httpServletRequest.getHeader("token");
        String url = httpServletRequest.getServletPath();
        if (ObjectUtil.equal(LOGIN_PROCESS_URL, url)
                || ObjectUtil.equal(LOGIN_IN_PROCESS_URL, url)
                || ObjectUtil.equal("/v2/api-docs", url)
                || ObjectUtil.equal("/swagger-ui.html", url)
                || ObjectUtil.equal("/doc.html", url)
                || ObjectUtil.equal("/captcha/v1", url)
                || ObjectUtil.equal("/file/upload", url)
                || ObjectUtil.equal("/project/getOneModel", url)
                || url.startsWith("/swagger-resources")
                || url.startsWith("/webjars")) {
            filterChain.doFilter(httpServletRequest, httpServletResponse);
        } else {
            Object o = cacheService.get(token);
            if (ObjectUtil.isEmpty(o)) {
                httpServletResponse.setStatus(200);
                httpServletResponse.setCharacterEncoding("utf-8");
                httpServletResponse.getWriter().write(JSON.toJSONString(Result.fail(RStateEnum.NO_AUTH.getCode(), "token 非法")));
            } else {
                System.out.println(url);
                LoginUser loginUser = JSONUtil.toBean(o + "", LoginUser.class);
                UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
                SecurityContextHolder.getContext().setAuthentication(authenticationToken);
                filterChain.doFilter(httpServletRequest, httpServletResponse);
            }
        }
    }
}
